﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace Team6_Algorithms.Converter
{
    /// <summary>
    /// Made by Dylan Snel
    /// </summary>
    public static class Converter
    {

        public static unsafe int[,] To2dIntArray(this Bitmap B)
        {
            int[,] Temp = new int[B.Width, B.Height];

             int pointerShift;
            Bitmap bSrc = (Bitmap)B.Clone();
            BitmapData BmData = B.LockBits(new Rectangle(0, 0, B.Width, B.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            
            int stride = BmData.Stride;
            int nWidth = B.Width;
            int nHeight = B.Height;
       

            System.IntPtr Scan0 = BmData.Scan0;

            unsafe
            {
                byte* gScan0 = (byte*)(void*)Scan0;

                if (gScan0[0] != gScan0[1])
                    throw new Exception("ImageNotGrayScaleException");

                int x = 0, y = 0;
                for (y = 0; y < nHeight; y++)
                {
                    for (x = 0; x < nWidth; x++)
                    {
                        pointerShift = (y * stride) + (x * 3);
                        Temp[x, y] = gScan0[pointerShift];
                    }
                }
                B.UnlockBits(BmData);

            }
            
            return Temp;
        }
    }
}
